package com.aic.aicdetactor;

import android.os.Looper;

import java.util.List;

import com.aic.aicdetactor.common.Constants;
import com.aic.aicdetactor.util.LogUtil;
import com.aic.xj.app.ble.BluetoothRequest;
import com.aic.xj.app.sdk.HandlerConstants;
import com.aic.xj.app.sdk.SensorOperator;

public class DataRequestThread extends Thread {

	private int _interval = 1000;
	private String _device = null;
	private boolean _stopFlag = false;
	private List<BluetoothRequest> _requests = null;
	private boolean _stopSend=false;
	private SensorOperator _operator = null;

	public  DataRequestThread(SensorOperator operator, String device, List<BluetoothRequest> requests, int interval) {
		this._operator = operator;
		this._interval = interval;
		this._device = device;
		this._requests = requests;
	}

	public boolean init(int timeout) {
		try {
			String result = _operator.open(_device, timeout);
			if (result.equals("#OK")) {
				return true;
			} else {
				_operator.get_handler().obtainMessage(HandlerConstants.HANDLER_ERROR, 0, 0, "连接失败:" + result).sendToTarget();
			}
		} catch (Exception e) {
			_operator.get_handler().obtainMessage(HandlerConstants.HANDLER_ERROR, 0, 0, e.getMessage()).sendToTarget();
		}
		return false;
	}
	public boolean init(int timeout,int connectTimeout) {//2016-05-23 liuhg添加
		try {
			String result = _operator.open(_device, timeout,connectTimeout);
			if (result.equals("#OK")) 
			{
				return true;
			} else {
				_operator.get_handler().obtainMessage(HandlerConstants.HANDLER_ERROR, 0, 0, "连接失败:" + result).sendToTarget();
			}
		} catch (Exception e) {
			_operator.get_handler().obtainMessage(HandlerConstants.HANDLER_ERROR, 0, 0,e.getMessage()).sendToTarget();
		}
		return false;
	}
	public void run() {
		Looper.prepare();
		while (!_stopFlag) {
			try {
				Thread.sleep(_interval);
				send();
			} catch (Exception e) {
				LogUtil.GetInstance().append(e);
//				Looper.prepare();
				_operator.get_handler().obtainMessage(HandlerConstants.HANDLER_ERROR, 0, 0, "异常，数据请求停止：" + e.getMessage()).sendToTarget();
				return;
			}
		}
	}

	private void send() throws Exception {
//		for (BluetoothRequest request : _requests) {
//			if (!_stopFlag) {
//				request._packageReceiveCount = 0;
//				byte[] response = _operator.ioControl(request._requestType, request._requestData, request._timeoutMilliseconds, request._responseLength);
//				_operator.get_handler().obtainMessage(Constants.HANDLER_IOCONTROL, request._requestType, request._packageReceiveCount, response).sendToTarget();
//			}
//		}
		if (!_stopSend) {
			for (BluetoothRequest request : _requests) {
				if (!_stopFlag) {
					request._packageReceiveCount = 0;
					byte[] response = _operator.ioControl(request._requestType, request._requestData, request._timeoutMilliseconds, request._responseLength);
					_operator.get_handler().obtainMessage(Constants.HANDLER_IOCONTROL, request._requestType, request._packageReceiveCount, response).sendToTarget();
				}
			}
		}
	}

	public void cancel() {
		_stopFlag = true;
		if (_operator != null) {
			_operator.close();
		}
	}
	public void onPause(){
		_stopSend=true;
	}
	public void onResume(){
		_stopSend=false;
	}
}
